<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/25 0025
 * Time: 11:27
 */

namespace app\admin\controller;


use app\model\model\Ad_type;
use think\Request;

class Ad extends BaseController
{
    protected $_Ad;
    protected $_Ad_type;
    public function __construct(Request $request = null){
        parent::__construct($request);
        $this->_Ad = new \app\model\model\Ad();
        $this->_Ad_type = new Ad_type();
    }
    /*=======================广告分类===========================*/
    public function typelists(){
        return $this->fetch();
    }
    public function getListType(){
        $where = [];
        $name = input('name');
        if($name){
            $where['name'] = ['like',"%$name%"];
        }
        $data = $this->_Ad_type->getList($where,input('limit'));
        foreach ($data as $v){
            $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
        }
        $list = $data->toArray();
        echo json_echo($list);
    }
    /**
     * 添加
     * */
    public function typeadd(){
        if(!Request::instance()->post()){
            return $this->fetch();
        }else{
            $data = [
                'name'=>input('name'),

                'create_time'=>time(),
            ];
            $re = $this->_Ad_type->add($data);
            if($re){
                return json_echo_msg(200,'添加成功');
            }else{
                return json_echo_msg(500,'添加失败');
            }
        }
    }
    /**
     * 修改
     * */
    public function typeup(){
        if(!Request::instance()->post()){
            $data = $this->_Ad_type->getOne(input('id'));
            $this->assign('data',$data);
            return $this->fetch();
        }else{
            $data = [
                'name'=>input('name'),
            ];
            $re = $this->_Ad_type->up(['id'=>input('id')],$data);
            if($re){
                return json_echo_msg(200,'修改成功');
            }else{
                return json_echo_msg(500,'修改失败');
            }
        }
    }
    /**
     * 获取一条数据
     * */
    public function getOneType(){
        $data = $this->_Ad_type->getOne(input('id'));

        echo json_echo_msg(200,'成功',$data);
    }
    /**
     * 删除
     * */
    public function delType(){

        $re = $this->_Ad_type->del(['id'=>input('id')]);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }
    /**
     * 批量删除
     * */
    public function delAllType(){
        $re = $this->_Ad_type->delAll(input()['data']);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }

    /*=======================广告===========================*/
    public function lists(){
        $ftype = input('type');
        $data = $this->_Ad_type->getListSelect();
        $this->assign('type',$data);
        $this->assign('ftype',$ftype);
        return $this->fetch();
    }
    public function getList(){
        $where = [];
        $name = input('name');
        if($name){
            $where['a.name'] = ['like',"%$name%"];
        }
        $type = input('type');
        if($type){
            $where['a.type_id'] = ['in',$type];
        }
        $where['a.flag'] = 1;
        $data = $this->_Ad->getList($where,input('limit'));

        foreach ($data as $v){
            $v['img_url']='<img style="height: 50px;width: 80px;" src="'.$v['img_url'].'">';
            $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
        }
        $list = $data->toArray();
        echo json_echo($list);
    }
    /**
     * 添加
     * */
    public function add(){
        if(!Request::instance()->post()){
            $data = $this->_Ad_type->getListSelect();

            $this->assign('type',$data);
            return $this->fetch();
        }else{

            $data = [
                'name'=>input('name'),
                'url'=>input('url'),
                'img_url'=>input('img_url'),
                'type_id'=>input('type_id'),
                'content'=>input('editorValue'),
                'create_time'=>time(),
                'alt'=>input('alt')
            ];
            $re = $this->_Ad->add($data);
            if($re){
                return json_echo_msg(200,'添加成功');
            }else{
                return json_echo_msg(500,'添加失败');
            }
        }
    }
    /**
     * 修改
     * */
    public function up(){
        if(!Request::instance()->post()){
            $data1 = $this->_Ad_type->getListSelect();
            $this->assign('type',$data1);
            $data = $this->_Ad->getOne(input('id'));
            $this->assign('data',$data);
            return $this->fetch();
        }else{
            $data = [
                'name'=>input('name'),
                'url'=>input('url'),
                'type_id'=>input('type_id'),
                'content'=>input('editorValue'),
                'create_time'=>time(),
                'alt'=>input('alt')
            ];
            if(input('img_url')){
                $data['img_url'] = input('img_url');
            }
            $re = $this->_Ad->up(['id'=>input('id')],$data);
            if($re){
                return json_echo_msg(200,'修改成功');
            }else{
                return json_echo_msg(500,'修改失败');
            }
        }
    }
    /**
     * 获取一条数据
     * */
    public function getOne(){
        $data = $this->_Ad->getOne(input('id'));
        $data['img_url']='<img style="height: 50px;width: 80px;" src="'.$data['img_url'].'">';
        echo json_echo_msg(200,'成功',$data);
    }
    /**
     * 删除
     * */
    public function del(){
        $data = ['flag'=>3];
        $re = $this->_Ad->up(['id'=>input('id')],$data);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }
    /**
     * 批量删除
     * */
    public function delAll(){
        $data = ['flag'=>3];
        $re = $this->_Ad->upAll(input()['data'],$data);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }
    /**
     * 排序
     * */
    public function sort(){
        $data = ['sort'=>input('sort')];
        $re = $this->_Ad->up(['id'=>input('id')],$data);
        if($re){
            return json_echo_msg(200,'成功！');
        }else{
            return json_echo_msg(400,'失败！');
        }
    }
    /*==========================回收站==============================================*/
    /**
     * 回收站页面
     * */
    public function recycled(){
        return $this->fetch();
    }
    /**
     * 回收站列表数据
     * */
    public function getRecycledList(){
        $where = [];
        $name = input('name');
        if($name){
            $where['a.name'] = ['like',"%$name%"];
        }
        $type = input('type');
        if($type){
            $where['a.type_id'] = ['like',"%$type%"];
        }
        $where['a.flag'] = 3;
        $data = $this->_Ad->getList($where,input('limit'));
        foreach ($data as $v){
            $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            $v['img_url']='<img style="height: 50px;width: 80px;" src="'.$v['img_url'].'">';
        }
        $list = $data->toArray();
        echo json_echo($list);
    }
    /**
     * 完全删除
     * */
    public function realDell(){
        $re = $this->_Ad->del(['id'=>input('id')]);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }
    /**
     * 批量完全删除
     * */
    public function realDellAll(){
        $re = $this->_Ad->delAll(input()['data']);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }
    /**
     * 恢复
     * */
    public function regain(){
        $data = ['flag'=>1];
        $re = $this->_Ad->up(['id'=>input('id')],$data);
        if($re){
            return json_echo_msg(200,'恢复成功！');
        }else{
            return json_echo_msg(400,'恢复失败！');
        }
    }
    /**
     * 批量恢复
     * */
    public function regainAll(){
        $data = ['flag'=>1];
        $re = $this->_Ad->upAll(input()['data'],$data);
        if($re){
            return json_echo_msg(200,'恢复成功！');
        }else{
            return json_echo_msg(400,'恢复失败！');
        }
    }
}